package com.zy.baselib.tools;


import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.zy.baselib.R;

public class ActivityUtils {

    private ActivityUtils() {}


    /**
     * 无动画
     * @param fm
     * @param fragment
     * @param id
     */
    public static void startFragment2Activity(FragmentManager fm, Fragment fragment, @IdRes int id) {
        if(fragment.isAdded()) {return;}//判断这个fragment是否被添加过
        FragmentTransaction ft = fm.beginTransaction(); //Fragment管理者启动一个事务加载一个 fragment，参数是一个fragment对象和布局文件中的fragment标签id
        ft.add(id, fragment);
        ft.commitAllowingStateLoss();//提交一个事务
    }

    public static void startFragment2Activity4RightEnter(FragmentManager fm, Fragment fragment, @IdRes int id) {
        if(fragment != null) {
            if(fragment.isAdded()) {return;}
            try {
                fm.beginTransaction().remove(fragment).commitAllowingStateLoss();
            } catch (Exception e) {

            }
            FragmentTransaction ft = fm.beginTransaction();
            ft.addToBackStack(null);
            ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out, R.anim.slide_right_in,
                    R.anim.slide_right_out);//设置Fragment添加的过度动画
            ft.add(id, fragment);
            ft.commitAllowingStateLoss();
        }
    }

    public static void startFragment2Activity4RightExit(FragmentManager fm, Fragment fragment, @IdRes int id) {
        if(fragment != null) {
            if(fragment.isAdded()) {return;}
            try {
                fm.beginTransaction().remove(fragment).commitAllowingStateLoss();
            } catch (Exception e) {

            }
            FragmentTransaction ft = fm.beginTransaction();
            ft.addToBackStack(null);
            ft.setCustomAnimations(0, R.anim.slide_right_out, 0,
                    R.anim.slide_right_out);//设置Fragment添加的过度动画
            ft.add(id, fragment);
            ft.commitAllowingStateLoss();
        }
    }
}
